package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ResPackageDish;
import com.ruoyi.system.service.IResPackageDishService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 菜品管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-02
 */
@RestController
@RequestMapping("/system/respakdish")
public class ResPackageDishController extends BaseController
{
    @Autowired
    private IResPackageDishService resPackageDishService;

    /**
     * 查询菜品管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:respakdish:list')")
    @GetMapping("/list")
    public AjaxResult list(ResPackageDish resPackageDish)
    {
        List<ResPackageDish> list = resPackageDishService.selectResPackageDishList(resPackageDish);
        System.out.println("ResPackageDishList:" + list);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:respakdish:list')")
    @GetMapping("/list1")
    public AjaxResult list1(ResPackageDish resPackageDish, @RequestParam(required = false) Long resId)
    {
        List<ResPackageDish> list = resPackageDishService.selectResPackageDishListByResId(resPackageDish, resId);
        List<ResPackageDish> allData = new ArrayList<>();
        for (ResPackageDish parent : list) {
            allData.add(parent);
            // 递归查询子数据
            findChildren(parent, allData);
        }
        System.out.println("ResPackageDishList:" + allData);
        return success(allData);
    }

    @PreAuthorize("@ss.hasPermi('system:respakdish:list')")
    @GetMapping("/list2")
    public AjaxResult list2(ResPackageDish resPackageDish, @RequestParam(required = false) Long userId)
    {
        List<ResPackageDish> list = resPackageDishService.selectResPackageDishListByUserId(resPackageDish, userId);
        List<ResPackageDish> allData = new ArrayList<>();
        for (ResPackageDish parent : list) {
            allData.add(parent);
            // 递归查询子数据
            findChildren(parent, allData);
        }
        System.out.println("ResPackageDishList:" + allData);
        return success(allData);
    }

//    @PreAuthorize("@ss.hasPermi('system:respakdish:list')")
    @GetMapping("/list3")
    public AjaxResult list3(ResPackageDish resPackageDish, @RequestParam(required = false) Long packageId)
    {
        List<ResPackageDish> list = resPackageDishService.selectResPackageDishListByPackageId(resPackageDish, packageId);
        List<ResPackageDish> allData = new ArrayList<>();
        for (ResPackageDish parent : list) {
            allData.add(parent);
            // 递归查询子数据
            findChildren(parent, allData);
        }
        System.out.println("ResPackageDishList:" + allData);
        return success(allData);
    }

    private void findChildren(ResPackageDish parent, List<ResPackageDish> allData) {
        ResPackageDish query = new ResPackageDish();
        query.setpId(parent.getTreeId());
        List<ResPackageDish> children = resPackageDishService.selectResPackageDishList(query);
        for (ResPackageDish child : children) {
            allData.add(child);
            // 递归查询子数据的子数据
            findChildren(child, allData);
        }
    }

    /**
     * 导出菜品管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:respakdish:export')")
    @Log(title = "菜品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResPackageDish resPackageDish)
    {
        List<ResPackageDish> list = resPackageDishService.selectResPackageDishList(resPackageDish);
        ExcelUtil<ResPackageDish> util = new ExcelUtil<ResPackageDish>(ResPackageDish.class);
        util.exportExcel(response, list, "菜品管理数据");
    }

    /**
     * 获取菜品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:respakdish:query')")
    @GetMapping(value = "/{treeId}")
    public AjaxResult getInfo(@PathVariable("treeId") Long treeId)
    {
        return success(resPackageDishService.selectResPackageDishByTreeId(treeId));
    }

    /**
     * 新增菜品管理
     */
    @PreAuthorize("@ss.hasPermi('system:respakdish:add')")
    @Log(title = "菜品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResPackageDish resPackageDish)
    {
        return toAjax(resPackageDishService.insertResPackageDish(resPackageDish));
    }

    /**
     * 修改菜品管理
     */
    @PreAuthorize("@ss.hasPermi('system:respakdish:edit')")
    @Log(title = "菜品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResPackageDish resPackageDish)
    {
        return toAjax(resPackageDishService.updateResPackageDish(resPackageDish));
    }

    /**
     * 删除菜品管理
     */
    @PreAuthorize("@ss.hasPermi('system:respakdish:remove')")
    @Log(title = "菜品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{treeIds}")
    public AjaxResult remove(@PathVariable Long[] treeIds)
    {
        return toAjax(resPackageDishService.deleteResPackageDishByTreeIds(treeIds));
    }
}
